let app = getApp();
let ischeckCard = '';

Component({
  properties: {
    users: {  // 用户信息
      type: Object,
      value: [],
      observer: function(newVal, oldVal, changedPath) {
        this.setData({ user: newVal });
      },
    },
    openId: {
      type: String,
      value: '',
      observer: function(newVal, oldVal, changedPath) {
        this.setData({ openid: newVal });
      },
    },
    moduleName: {  // 模块名称
      type: String,
      value: '',
    },
    memberShipPath: {  // 会员须知页面地址 【绝对路径】
      type: String,
      value: '',
    },
    rechargePath: {  // 会员充值页面地址 【绝对路径】
      type: String,
      value: '',
    },
    integralPath: {  // 积分页面地址 【绝对路径】
      type: String,
      value: '',
    },
  },
  data: {
    animationData: '',
    codeImg: false,
    openCard: false,  // 尚未开卡
    codeInfo: [],
    qrcode: '',
    user: [],
    openid: '',
  },
  methods: {
    // 开卡接口
    _activateCard() {
      let that = this;
      wx.showNavigationBarLoading();
      wx.showLoading({
        title: '处理中...',
        mask: true,
      });
      app.util.request({
        url: 'wxapp/wechatCard/activate',
        cachetime: 0,
        success: function(res) {
          let data = res.data.result;
          if (Number(res.data.errcode) === 0) {
            wx.navigateToMiniProgram({
              appId: 'wxeb490c6f9b154ef9',
              extraData: {
                encrypt_card_id: data.encrypt_card_id,
                outer_str: data.outer_str,
                biz: data.biz,
              },
              success: function() {
                wx.hideNavigationBarLoading();
                wx.hideLoading();
                ischeckCard = setInterval(function() {
                  that._checkCard();
                }, 1000);
              },
              fail: function(res) {
                that._showToast(res);
              },
            });
          } else {
            that._showToast(res);
          }
        },
        fail: function(res) {
          that._showToast(res);
        },
      });
    },
  
    // 检测会员卡接口
    _checkCard() {
      let that = this;
      let unionid = wx.getStorageSync('user_info').unionid;
      let objdata = {
          m: that.data.moduleName,
          openid: that.data.openid,
        };
      if (unionid) objdata.unionid = unionid;

      app.util.request({
        url: 'wxapp/wechatCard/checkCard',
        cachetime: 0,
        data: objdata,
        success: function(res) {
          // errcode : -1 未开卡  -2 待激活
          let state = Number(res.data.errcode);
          that.setData({ codeInfo: res.data.result ? res.data.result : '' });
          if (state === -1) {
            that.setData({ openCard: false });
          }
          else if (state === -2) {
            that._activeCard();
          } else {
            that.setData({ openCard: true });
            that._qrcode();
            clearInterval(ischeckCard);
          }
        },
      });
    },
    // 激活会员卡
    _activeCard() {
      let that = this;
      app.util.request({
        url: 'wxapp/wechatCard/activeCard',
        cachetime: 0,
        data: {
          openid: that.data.openid,
        },
        success: function(res) {
          that.setData({
            openCard: true,
          });
        },
        fail: function(res) {
          console.log('激活会员卡阶段失败');
        },
      });
    },
    // 获取二维码地址
    _qrcode() {
      let that = this;
      app.util.request({
        url: 'wxapp/wechatCard/qrcode',
        data: {
          openid: that.data.openid,
        },
        cachetime: 0,
        success: function(res) {
          that.setData({
            qrcode: res.data.result.qrcode_url,
          });
        },
      });
    },
  
    _jumpPage(e) {
      let { url } = e.currentTarget.dataset;
      if (!url) return;
      app.util.navigateTo(url, false);
    },
  
    _fireScene() {
      let that = this;
      that._fireCardComm(180, true, 150);
    },
  
    _fireReset() {
      let that = this;
      that._fireCardComm(0, false, 110);
    },
  
    _fireCardComm(deg, bool, time) {
      let that = this;
      if (!that.data.openCard) return;
      that.animation.rotateY(deg).step();
      that.setData({
        animationData: that.animation.export(),
      });
      setTimeout(function() {
        that.setData({ codeImg: bool });
      }, time);
    },

    _showToast(res) {
      wx.hideNavigationBarLoading();
      wx.hideLoading();
      wx.showToast({
        title: res.data.errmsg || '开卡失败!',
        icon: 'none',
        duration: 1500,
        mask: true,
      });
    },
  },
  attached() {
    let that = this;
    let user = that.data.user;
    that.setData({
      user: user,
    });
    let animation = wx.createAnimation({
      duration: 800,
      timingFunction: 'ease',
    });
    that.animation = animation;
  },

  ready() {
      this._checkCard();
    },

  detached() {
    clearInterval(ischeckCard);
  },
});